﻿using System;
using System.Threading;
class EventListener
{
  private ListWithChangedEvent List;

  public EventListener(ListWithChangedEvent list)
  {
    List = list;
    // Add "ListChanged" to the Changed event on "List".
    List.Changed += new ChangedEventHandler(ListChanged);
  }

  // This will be called whenever the list changes.
  private void ListChanged(object sender, EventArgs e)
  {
    Console.BackgroundColor = ConsoleColor.DarkGreen;
    Console.ForegroundColor = ConsoleColor.Yellow;
    Console.WriteLine("New City Added");
    Console.ResetColor();
  }

  public void Detach()
  {
    // Detach the event and delete the list
    List.Changed -= new ChangedEventHandler(ListChanged);
    List = null;
  }
}